import os
import shutil
from pathlib import Path


# 路径配置
path_A = '/home/champrin/Desktop/record_data/网盘/checked/411armor_411bluearmor_42_552__'  # 存放文件名的目录
path_B = '/home/champrin/Desktop/record_data/网盘/checked/411armor_img'  # 搜索文件的目录
path_C = '/home/champrin/Desktop/record_data/网盘/checked/411armor_411bluearmor_42_552___extract'  # 存放结果的目录
os.makedirs(path_C, exist_ok=True)

# 获取路径 A 下的所有文件名
def get_filenames_from_A(path_A):
    filenames = []
    for root, dirs, files in os.walk(path_A):
        for file in files:
            filenames.append(file)
    return filenames

# 在路径 B 中查找并复制到路径 C
def find_and_copy(filenames, path_B, path_C):
    for root, dirs, files in os.walk(path_B):
        for file in files:
            if file in filenames:
                # 构建原始文件路径和目标文件路径
                source_file = os.path.join(root, file)
                relative_path = os.path.relpath(root, path_B)
                target_dir = os.path.join(path_C, relative_path)
                target_file = os.path.join(target_dir, file)

                # 创建目标目录（如果不存在）
                Path(target_dir).mkdir(parents=True, exist_ok=True)

                # 复制文件
                shutil.move(source_file, target_file)
                print(f"已复制: {source_file} -> {target_file}")

if __name__ == '__main__':
    filenames = get_filenames_from_A(path_A)
    find_and_copy(filenames, path_B, path_C)
